﻿<%@ Page Title="" Language="C#" MasterPageFile="~/ASP/HomeSite.Master" AutoEventWireup="true"
    CodeBehind="MyOrder.aspx.cs" Inherits="Agricultural_Sales.ASP.MyOrder" %>
    <asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
        <style>
            .order-container {
                max-width: 1400px;
                margin: 20px auto;
                padding: 20px;
                background: #fff;
                border-radius: 8px;
                box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
            }

            .order-header {
                text-align: center;
                margin-bottom: 30px;
                padding-bottom: 15px;
                border-bottom: 2px solid #007bff;
            }

            .order-title {
                color: #007bff;
                font-size: 28px;
                margin: 0;
            }

            .styled-gridview {
                width: 100%;
                border-collapse: collapse;
                margin-bottom: 20px;
                background: #fff;
                border-radius: 8px;
                overflow: hidden;
                box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
            }

            .styled-gridview th {
                background: linear-gradient(135deg, #007bff, #0056b3);
                color: white;
                padding: 15px 8px;
                text-align: center;
                font-weight: 600;
                border: none;
                font-size: 14px;
            }

            .styled-gridview td {
                padding: 12px 8px;
                text-align: center;
                border-bottom: 1px solid #e9ecef;
                vertical-align: middle;
                font-size: 13px;
            }

            .styled-gridview tr:hover {
                background-color: #f8f9fa;
            }

            .product-list {
                max-width: 200px;
                text-align: left;
                line-height: 1.4;
            }

            .product-item {
                display: block;
                padding: 2px 0;
                color: #495057;
                border-bottom: 1px dotted #dee2e6;
            }

            .product-item:last-child {
                border-bottom: none;
            }

            .payment-info {
                background: #f8f9fa;
                padding: 20px;
                border-radius: 8px;
                border: 1px solid #e9ecef;
                margin-top: 20px;
                max-width: 500px;
                margin-left: auto;
                margin-right: auto;
            }

            .payment-title {
                text-align: center;
                color: #495057;
                margin-bottom: 20px;
                font-size: 18px;
            }

            .payment-grid {
                display: grid;
                grid-template-columns: 120px 1fr;
                gap: 15px;
                align-items: center;
            }

            .payment-label {
                font-weight: 500;
                color: #495057;
            }

            .payment-value {
                color: #212529;
            }

            .btn-payment {
                background: #28a745;
                color: white;
                border: none;
                padding: 6px 12px;
                border-radius: 4px;
                cursor: pointer;
                font-size: 12px;
            }

            .btn-payment:hover {
                background: #218838;
            }

            .order-state {
                padding: 4px 8px;
                border-radius: 4px;
                font-size: 12px;
                font-weight: 500;
            }

            .state-pending {
                background: #fff3cd;
                color: #856404;
            }

            .state-paid {
                background: #d4edda;
                color: #155724;
            }

            .state-shipped {
                background: #cce7ff;
                color: #004085;
            }

            .state-completed {
                background: #d1ecf1;
                color: #0c5460;
            }

            .state-cancelled {
                background: #f8d7da;
                color: #721c24;
            }

            .btn-action {
                margin: 2px;
                padding: 4px 8px;
                border: none;
                border-radius: 4px;
                cursor: pointer;
                font-size: 12px;
                min-width: 60px;
            }

            .btn-pay {
                background: #28a745;
                color: white;
            }

            .btn-cancel {
                background: #dc3545;
                color: white;
            }

            .btn-refund {
                background: #ffc107;
                color: #212529;
            }

            .btn-confirm {
                background: #17a2b8;
                color: white;
            }

            .btn-disabled {
                background: #6c757d;
                color: white;
                cursor: not-allowed;
            }
        </style>
    </asp:Content>
    <asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
        <div class="order-container">
            <div class="order-header">
                <h2 class="order-title">📦 我的订单</h2>
            </div>

            <asp:GridView ID="GridViewMyOrders" runat="server" AutoGenerateColumns="False" DataKeyNames="OrderId"
                DataSourceID="SqlDataSourceMyOrders" CssClass="styled-gridview" GridLines="None" BorderStyle="None">
                <Columns>
                    <asp:TemplateField HeaderText="支付信息">
                        <ItemTemplate>
                            <asp:Button ID="btnShowPayment" runat="server" Text="💳 支付信息" CommandName="ShowPayment"
                                CommandArgument='<%# Eval("OrderId") %>' OnClick="btnShowPayment_Click"
                                CssClass="btn-payment" />
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:BoundField DataField="OrderId" HeaderText="订单编号" ReadOnly="True" />
                    <asp:TemplateField HeaderText="购买商品">
                        <ItemTemplate>
                            <div class="product-list">
                                <%# Eval("ProductNames") %>
                            </div>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:BoundField DataField="OrderNum" HeaderText="商品数量" />
                    <asp:TemplateField HeaderText="订单状态">
                        <ItemTemplate>
                            <span class='order-state <%# GetOrderStateClass(Eval("OrderState").ToString()) %>'>
                                <%# Eval("OrderState") %>
                            </span>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:BoundField DataField="TotalAmout" HeaderText="总金额" DataFormatString="{0:C}" />
                    <asp:BoundField DataField="DeliveryAddr" HeaderText="收货地址" />
                    <asp:BoundField DataField="ContactPhone" HeaderText="联系电话" />
                    <asp:BoundField DataField="UpdateAt" HeaderText="更新时间" DataFormatString="{0:yyyy-MM-dd HH:mm}" />

                    <asp:TemplateField HeaderText="操作">
                        <ItemTemplate>
                            <div style="display: flex; flex-direction: column; gap: 2px;">
                                <asp:Button ID="btnPay" runat="server" Text="💰 付款" CommandName="Pay"
                                    CommandArgument='<%# Eval("OrderId") %>' OnClick="btnOrderAction_Click"
                                    CssClass="btn-action btn-pay"
                                    Visible='<%# Eval("OrderState").ToString() == "待支付" %>' />

                                <asp:Button ID="btnCancelUnpaid" runat="server" Text="❌ 取消" CommandName="Cancel"
                                    CommandArgument='<%# Eval("OrderId") %>' OnClick="btnOrderAction_Click"
                                    CssClass="btn-action btn-cancel" OnClientClick="return confirm('确定要取消这个订单吗？');"
                                    Visible='<%# Eval("OrderState").ToString() == "待支付" %>' />

                                <asp:Button ID="btnCancelPaid" runat="server" Text="❌ 取消" CommandName="Cancel"
                                    CommandArgument='<%# Eval("OrderId") %>' OnClick="btnOrderAction_Click"
                                    CssClass="btn-action btn-cancel"
                                    OnClientClick="return confirm('确定要取消这个订单吗？取消后将申请退款。');"
                                    Visible='<%# Eval("OrderState").ToString() == "已支付" %>' />

                                <asp:Button ID="btnRefund" runat="server" Text="💸 申请退款" CommandName="Refund"
                                    CommandArgument='<%# Eval("OrderId") %>' OnClick="btnOrderAction_Click"
                                    CssClass="btn-action btn-refund" OnClientClick="return confirm('确定要申请退款吗？');"
                                    Visible='<%# Eval("OrderState").ToString() == "已发货" %>' />

                                <asp:Button ID="btnConfirm" runat="server" Text="✅ 确认收货" CommandName="Confirm"
                                    CommandArgument='<%# Eval("OrderId") %>' OnClick="btnOrderAction_Click"
                                    CssClass="btn-action btn-confirm" OnClientClick="return confirm('确定已收到货物吗？');"
                                    Visible='<%# Eval("OrderState").ToString() == "已发货" %>' />

                                <span class="btn-action btn-disabled"
                                    style='<%# (Eval("OrderState").ToString() == "已取消" || Eval("OrderState").ToString() == "已完成") ? "display:inline-block;" : "display:none;" %>'>
                                    <%# Eval("OrderState").ToString()=="已取消" ? "已取消" : "已完成" %>
                                </span>
                            </div>
                        </ItemTemplate>
                    </asp:TemplateField>
                </Columns>
                <EmptyDataTemplate>
                    <div style="text-align: center; padding: 40px; color: #6c757d;">
                        <h4>📋 暂无订单</h4>
                        <p>您还没有任何订单，快去选购商品吧！</p>
                    </div>
                </EmptyDataTemplate>
            </asp:GridView>

            <!-- 支付信息显示区域 -->
            <div class="payment-info" id="paymentInfo" runat="server" visible="false">
                <h4 class="payment-title">💳 支付信息详情</h4>
                <asp:FormView ID="FormViewPayment" runat="server" DataKeyNames="PayId"
                    DataSourceID="SqlDataSourcePayment">
                    <ItemTemplate>
                        <div class="payment-grid">
                            <span class="payment-label">支付编号：</span>
                            <span class="payment-value">
                                <%# Eval("PayId") %>
                            </span>

                            <span class="payment-label">订单编号：</span>
                            <span class="payment-value">
                                <%# Eval("OrderId") %>
                            </span>

                            <span class="payment-label">支付方式：</span>
                            <span class="payment-value">
                                <%# Eval("PayMethod") %>
                            </span>

                            <span class="payment-label">支付金额：</span>
                            <span class="payment-value" style="color: #dc3545; font-weight: bold;">
                                <%# Eval("PayAmout", "{0:C}" ) %>
                            </span>

                            <span class="payment-label">支付状态：</span>
                            <span class="payment-value">
                                <%# Eval("PayState") %>
                            </span>

                            <span class="payment-label">支付时间：</span>
                            <span class="payment-value">
                                <%# Eval("PayTime", "{0:yyyy-MM-dd HH:mm:ss}" ) %>
                            </span>
                        </div>
                    </ItemTemplate>
                    <EmptyDataTemplate>
                        <div style="text-align: center; color: #6c757d; padding: 20px;">
                            <p>📋 暂无支付信息</p>
                            <p>该订单还没有支付记录</p>
                        </div>
                    </EmptyDataTemplate>
                </asp:FormView>
            </div>

            <!-- 数据源 -->
            <asp:SqlDataSource ID="SqlDataSourceMyOrders" runat="server"
                ConnectionString="<%$ ConnectionStrings:AgriculturalSalesConnectionString %>"
                SelectCommand="SELECT DISTINCT o.OrderId, o.UserId, o.OrderNum, o.OrderState, o.TotalAmout, o.DeliveryAddr, o.ContactPhone, o.UpdateAt,
                              STUFF((
                                  SELECT ', ' + p.Name
                                  FROM OrderProduct op2
                                  INNER JOIN Product p ON op2.ProductId = p.ProductId
                                  WHERE op2.OrderId = o.OrderId
                                  FOR XML PATH('')
                              ), 1, 2, '') AS ProductNames
                              FROM [Order] o
                              WHERE o.UserId = @UserId
                              ORDER BY o.UpdateAt DESC">
                <SelectParameters>
                    <asp:SessionParameter Name="UserId" SessionField="UserId" Type="Int32" DefaultValue="0" />
                </SelectParameters>
            </asp:SqlDataSource>

            <asp:SqlDataSource ID="SqlDataSourcePayment" runat="server"
                ConnectionString="<%$ ConnectionStrings:AgriculturalSalesConnectionString %>"
                SelectCommand="SELECT * FROM [Payment] WHERE [OrderId] = @OrderId">
                <SelectParameters>
                    <asp:Parameter Name="OrderId" Type="Int32" DefaultValue="-1" />
                </SelectParameters>
            </asp:SqlDataSource>
        </div>
    </asp:Content>